import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs';

import { IFinancialManage } from 'app/shared/model/financial-manage.model';
import { FinancialManageService } from './financial-manage.service';

@Component({
    selector: 'jhi-financial-manage-update',
    templateUrl: './financial-manage-update.component.html'
})
export class FinancialManageUpdateComponent implements OnInit {
    private _financialManage: IFinancialManage;
    isSaving: boolean;

    constructor(private financialManageService: FinancialManageService, private activatedRoute: ActivatedRoute) {}

    ngOnInit() {
        this.isSaving = false;
        this.activatedRoute.data.subscribe(({ financialManage }) => {
            this.financialManage = financialManage;
        });
    }

    previousState() {
        window.history.back();
    }

    save() {
        this.isSaving = true;
        if (this.financialManage.id !== undefined) {
            this.subscribeToSaveResponse(this.financialManageService.update(this.financialManage));
        } else {
            this.subscribeToSaveResponse(this.financialManageService.create(this.financialManage));
        }
    }

    private subscribeToSaveResponse(result: Observable<HttpResponse<IFinancialManage>>) {
        result.subscribe((res: HttpResponse<IFinancialManage>) => this.onSaveSuccess(), (res: HttpErrorResponse) => this.onSaveError());
    }

    private onSaveSuccess() {
        this.isSaving = false;
        this.previousState();
    }

    private onSaveError() {
        this.isSaving = false;
    }
    get financialManage() {
        return this._financialManage;
    }

    set financialManage(financialManage: IFinancialManage) {
        this._financialManage = financialManage;
    }
}
